<?php

class Menu_AdminController extends AdminController {
    /**
     * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
     * using two-column layout. See 'protected/views/layouts/column2.php'.
     */
    //public $layout = '//layouts/column2';

    /**
     * @return array action filters
     */
    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    /**
     * Displays a particular model.
     * @param integer $id the ID of the model to be displayed
     */
    public function actionIndex() {
        $model = site_administrator_menu::listData();
        $this->render('admin', array(
            'data' => $model,
        ));
    }

    public function actionOrder_by() {
            
            //explode POST
            $var_order = explode("_", $_POST['id']);
        
            if ($var_order['0'] == 'up') {
                $condition = '<=';
                $sort = 'DESC';
            } elseif ($var_order['0'] == 'down') {
                $condition = '>=';
                $sort = 'ASC';
            }
           
            //$par_id = $db->GetOne("SELECT menu_par_id FROM site_menu WHERE menu_id = '" . $id . "'");
            $par_id = Yii::app()->db->createCommand()
                    ->select('administrator_menu_par_id')
                    ->from('site_administrator_menu')
                    ->where('administrator_menu_id=:id', array(':id' => $var_order['1']))
                    ->order('administrator_menu_order_by DESC')
                    ->queryScalar();
           
            //$order_by = $db->GetOne("SELECT menu_order_by FROM site_menu WHERE menu_id = '" . $id . "'");
            $order_by = Yii::app()->db->createCommand()
                    ->select('administrator_menu_order_by')
                    ->from('site_administrator_menu')
                    ->where('administrator_menu_id=:id', array(':id' => $var_order['1']))
                    ->queryScalar();
           
            
            $sql_menu = "SELECT administrator_menu_id, administrator_menu_order_by
                  FROM site_administrator_menu
                  WHERE administrator_menu_order_by " . $condition . " " . $order_by . "
                  AND administrator_menu_id != '" . $var_order['1'] . "'
                  AND administrator_menu_par_id = '" . $par_id . "'
                  ORDER BY administrator_menu_order_by " . $sort . "
                  LIMIT 1";
            
            $row_menu = Yii::app()->db->createCommand( $sql_menu )->queryRow();
            echo "UPDATE site_administrator_menu SET administrator_menu_order_by = '" . $order_by . "' WHERE administrator_menu_id = '" . $row_menu['administrator_menu_id'] . "'";
            echo "<br>"."UPDATE site_administrator_menu SET administrator_menu_order_by = '" . $row_menu['administrator_menu_order_by'] . "' WHERE administrator_menu_id = '" . $var_order['1'] . "'";
            Yii::app()->db->createCommand("UPDATE site_administrator_menu SET administrator_menu_order_by = '" . $order_by . "' WHERE administrator_menu_id = '" . $row_menu['administrator_menu_id'] . "'")->execute();
            Yii::app()->db->createCommand("UPDATE site_administrator_menu SET administrator_menu_order_by = '" . $row_menu['administrator_menu_order_by'] . "' WHERE administrator_menu_id = '" . $var_order['1'] . "'")->execute();
        
              
    }

    public function actionCreate() {
        $model = new site_administrator_menu;

        if (isset($_POST['site_administrator_menu'])) {
            
            $order_by = Yii::app()->db->createCommand()
                    ->select('administrator_menu_order_by')
                    ->from('site_administrator_menu')
                    ->order('administrator_menu_order_by DESC')
                    ->queryScalar();
            
            $model->attributes = $_POST['site_administrator_menu'];

            $model->administrator_menu_is_active = "1";
            $model->administrator_menu_order_by = $order_by+1;


            if ($model->save()) {

                $this->redirect(array('index'));
            }
        }

        $this->render('create', array(
            'model' => $model,
        ));
    }

    public function actionUpdate($id) {
        $model = $this->loadModel($id);

        if (isset($_POST['site_administrator_menu'])) {
            $model->attributes = $_POST['site_administrator_menu'];
            if ($model->save())
                $this->redirect(array('update', 'id' => $model->administrator_menu_id));
        }

        $this->render('update', array(
            'model' => $model,
        ));
    }

    public function loadModel($id) {
        $model = site_administrator_menu::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'site-news-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

}

?>
